package communication.client.vertx.handler;

import communication.client.vertx.service.ClientHoleService;
import communication.common.business.base.CommonBaseHandler;
import communication.common.business.base.FcrSocketAddress;
import communication.common.business.base.MethodMap;
import communication.common.business.base.WorkModeEnum;
import communication.common.business.heartbeat.HeartBeat;
import communication.common.business.hole.DefaultHoleHandler;
import communication.common.business.hole.P2pHoleRequest;
import communication.common.business.heartbeat.HeartBeatAck;
import communication.common.business.heartbeat.HeartBeatServiceOfClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 处理网络
 * @author mac
 */
@Slf4j
@Component
public class ClientNetModuleHandler extends CommonBaseHandler {

    @Autowired
    private ClientHoleService netService;

    @Autowired
    private HeartBeatServiceOfClient heartBeatServiceOfClient;

    @Autowired
    private DefaultHoleHandler defaultHoleHandler;


    @PostConstruct
    private void init() throws Exception {
        // 预打洞
        MethodMap hole=MethodMap.builder()
                .op("hole")
                .serviceBean(netService)
                .methodName("hole")
                .type(0)
                .requestClassList(new Class[]{P2pHoleRequest.class})
                .responseClass(null)
                .build();

        addMethodMap(hole);


        // 处理心跳
        MethodMap heartBeat=MethodMap.builder()
                .op("heartBeat")
                .serviceBean(heartBeatServiceOfClient)
                .methodName("handleHeartBeat")
                .type(0)
                .requestClassList(new Class[]{HeartBeat.class, FcrSocketAddress.class})
                .responseClass(null)
                .build();

        addMethodMap(heartBeat);

        // 处理心跳的响应
        MethodMap heartBeatAck=MethodMap.builder()
                .op("heartBeatAck")
                .serviceBean(heartBeatServiceOfClient)
                .methodName("receiveAck")
                .type(0)
                .requestClassList(new Class[]{HeartBeatAck.class, FcrSocketAddress.class})
                .responseClass(null)
                .build();

        addMethodMap(heartBeatAck);


        // 处理打洞测试的请求
        MethodMap holeTest=MethodMap.builder()
                .op("test-hole")
                .serviceBean(defaultHoleHandler)
                .methodName("handleHoleTestReq")
                .type(0)
                .requestClassList(new Class[]{Integer.class, FcrSocketAddress.class})
                .responseClass(WorkModeEnum.class)
                .build();

        addMethodMap(holeTest);


        // 处理打洞测试请求的响应
        MethodMap holeTestResp=MethodMap.builder()
                .op("test-hole")
                .serviceBean(defaultHoleHandler)
                .methodName("handleHoleTestResp")
                .type(1)
                .requestClassList(new Class[]{WorkModeEnum.class, FcrSocketAddress.class})
                .responseClass(null)
                .build();

        addMethodMap(holeTestResp);


    }


    @Override
    public String getModuleName() {
        return "net";
    }
}
